/*
 * ConversionServDAO.java
 *
 * Created on March 22, 2007, 1:45 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.group1E.data;
import edu.byu.isys413.group1E.Cache;
import edu.byu.isys413.group1E.ConnectionPool;
import edu.byu.isys413.group1E.ConnectionPoolException;
import edu.byu.isys413.group1E.DataException;
import edu.byu.isys413.group1E.GUID;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author tkulbeth
 */

public class ConversionServDAO extends DAObject {
    
    private static ConversionServDAO instance = null;
    
    /** Creates a new instance of ConversionServDAO */
    public ConversionServDAO() {
    }
    
    
    
    public BusinessObject create() throws Exception{
        String id = GUID.generate();
        BusinessObject bo = new ConversionServBO(id);
        Cache c = Cache.getInstance();
        c.put(bo.getId(), bo);
        return bo;
    }
    
    
    public BusinessObject read(String id, Connection conn) throws SQLException, DataException {
        // check to see if id in the cache
        Cache c = Cache.getInstance();
        ConversionServBO bo = (ConversionServBO)c.get(id);
        if (bo == null) {
            // if not in the cache, get a result set from
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM CONVERSION_SERVICE WHERE CONVERSIONSERVICEID = '" + id + "'");
            conn.commit();
            
            //build the object from the result set
            if (rs.next()) {
                
                bo = new ConversionServBO     (rs.getString("CONVERSIONSERVICEID"));
                
                //BO POPULATION
                bo.setPrice              (rs.getDouble          ("PRICE"));
                bo.setType           (rs.getString     ("TYPE"));
                
                
                bo.setAlreadyInDB(true);
                bo.setIsDirty(false);
                stmt.close();
            }
        }
        // return the BO
        return bo;
    }
    
    
    
    public  ArrayList<BusinessObject> readAll() throws DataException, SQLException, ConnectionPoolException {
        ArrayList<BusinessObject> boList = new ArrayList<BusinessObject>();
        //now build the query
        String query = "SELECT * FROM CONVERSION_SERVICE";
        
        Connection conn = ConnectionPool.getInstance().get();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        conn.commit();
        
        //extract the elements from the result set and build a list of results
        //build the object from the result set
        while (rs.next()) {
            ConversionServBO bo = new ConversionServBO(rs.getString("CONVERSIONSERVICEID"));
            
            
            //BO POPULATION
            bo.setPrice              (rs.getDouble         ("PRICE"));
            bo.setType           (rs.getString     ("TYPE"));
            
            
            bo.setAlreadyInDB(true);
            bo.setIsDirty(false);
            boList.add(bo);
            
        }//while
        
        stmt.close();
        ConnectionPool.getInstance().release(conn);
        return boList;
    }
    
    
    
    public void update(BusinessObject bo, Connection conn) throws SQLException, DataException {
        //update the DB
        ConversionServBO BO = (ConversionServBO)bo;
        PreparedStatement pstmt = conn.prepareStatement("UPDATE CONVERSION_SERVICE  SET PRICE = ?, TYPE = ?  WHERE CONVERSIONSERVICEID = ?");
        
        
        //CUSTOM UPDATE STUFF
        pstmt.setDouble          (1, BO.getPrice());
        pstmt.setString     (2, BO.getType());
        
        pstmt.executeUpdate();
        pstmt.close();
        bo.setAlreadyInDB(true);
        bo.clean();
    }
    
    public void insert(BusinessObject bo, Connection conn) throws SQLException, DataException {
        
        //update the DB
        ConversionServBO BO = (ConversionServBO)bo;
        PreparedStatement pstmt = conn.prepareStatement("INSERT INTO CONVERSION_SERVICE (CONVERSIONSERVICEID, PRICE, TYPE)  VALUES ( ?, ?, ?)");
        
        //GETTER
        pstmt.setString     (1, BO.getId());
        pstmt.setDouble          (2, BO.getPrice());
        pstmt.setString     (3, BO.getType());
        pstmt.executeUpdate();
        pstmt.close();
        
        // tell the object that it's now in the db (so we call update next time not insert)
        bo.setAlreadyInDB(true);
        bo.clean();
        
    }
    
    
    
    
    
    
}
//